<%@ page language="java"  pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%> 
<%@ page errorPage="error.jsp" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<form action="jasper/getReport.action" enctype="multipart/form-data"
			method="POST">
			<input type="hidden" value="benke" name="reportname"/>
			<input type="hidden" value="本科生分布表" name="downloadFileName"/>
			<input type="submit" value="导出excel" name="sub"/>
</form>
<%@ include file="connectdb.jsp"%>
<%
	File reportFile = new File(application.getRealPath("/reports/benke.jasper"));
    if (!reportFile.exists())
		throw new JRRuntimeException("File benke_xueyuan.jasper not found. The report design must be compiled first.");

	JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

	Map parameters = new HashMap();
	parameters.put("ReportTitle", "Address Report");
	parameters.put("BaseDir", reportFile.getParentFile());
	parameters.put("OVERRIDE_Comparator", new Comparator() {
 
        private List list = new ArrayList();
 
        public int compare(Object o1, Object o2) {
          int o1_index = -1;
          int o2_index = -1;
          if ((o2_index = list.indexOf(o2)) == -1 && list.add(o2)) {
            o2_index = list.indexOf(o2);
          }
          if ((o1_index = list.indexOf(o1)) == -1 && list.add(o1)) {
            o1_index = list.indexOf(o1);
          }
 
          if (o1_index > o2_index) {
            return 1;
          } else if (o1_index < o2_index) {
            return -1;
          } else {
            return 0;
          }
        }
      });
				
	JasperPrint jasperPrint = 
		JasperFillManager.fillReport(
			jasperReport, 
			parameters, 
			conn
			);
				
	JRHtmlExporter exporter = new JRHtmlExporter();

	session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
	
	exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
	exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
	
	exporter.exportReport();
	conn.close();
%>

